package com.xckj.cycling.presenter;

import androidx.annotation.NonNull;
import com.xckj.cycling.bean.BaseBean;
import com.xckj.cycling.framework.net.RetrofitManager;
import com.xckj.cycling.global.Constant;
import com.xckj.cycling.iview.IForgetPasswordView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import java.util.Objects;

/**
 * @Author: ChenYao
 * @Describe: 忘记密码
 * @Date: 2023/3/5 21:01
 */
public class ForgetPasswordPresenter {
    private final IForgetPasswordView forgetPasswordView;

    public ForgetPasswordPresenter(IForgetPasswordView forgetPasswordView) {
        this.forgetPasswordView = forgetPasswordView;
    }

    /**
     * 忘记密码
     *
     * @param email       邮箱
     * @param captcha     验证码
     * @param newpassword 新密码
     */
    public void forgetPassword(String email, String captcha, String newpassword) {
        forgetPasswordView.showLoading();
        Call<BaseBean<String>> call = RetrofitManager.getInstance().getService().forgetPassword(email, captcha, newpassword);
        call.enqueue(new Callback<BaseBean<String>>() {
            @Override
            public void onResponse(@NonNull Call<BaseBean<String>> call, @NonNull Response<BaseBean<String>> response) {
                if (response.isSuccessful()) {
                    BaseBean<String> baseBean = response.body();
                    if (Constant.SUCCESS_CODE == Objects.requireNonNull(baseBean).code) {
                        forgetPasswordView.success(baseBean.msg);
                    } else {
                        forgetPasswordView.failure(baseBean.msg);
                    }
                } else {
                    forgetPasswordView.failure("request failure");
                }
                forgetPasswordView.hideDialog();
            }

            @Override
            public void onFailure(@NonNull Call<BaseBean<String>> call, @NonNull Throwable t) {
                t.printStackTrace();
                forgetPasswordView.failure("request failure");
                forgetPasswordView.hideDialog();
            }
        });
    }
}
